package com.dnui.seven2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnui.seven2.common.R;
import com.dnui.seven2.entity.Announcement;
import com.dnui.seven2.entity.Merch;
import com.dnui.seven2.service.AnnouncementService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;


    @GetMapping("/queryAnnouncements")
    public R<Page> queryAnnouncements(Integer currentPageNum, Integer everyPage){

        //构造分页构造器
        Page<Announcement> pageInfo=new Page(currentPageNum,everyPage);
        //构造条件构造器
        LambdaQueryWrapper<Announcement> queryWrapper=new LambdaQueryWrapper();

        //执行查询
        announcementService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @PostMapping("saveAnnouncement")
    public R<String> saveAnnouncement(@RequestBody Announcement announcement){
        announcementService.saveAnnouncement(announcement);
        return  R.success("添加成功");
    }

    @DeleteMapping("deleteAnnouncement/{announcementId}")
    public R<String> deleteAnnouncement( @PathVariable Integer announcementId){
        announcementService.deleteAnnouncement(announcementId);
        return R.success("删除成功");
    }

    @PutMapping("updateAnnouncement")
    public R<String> updateAnnouncement(@RequestBody Announcement announcement){
        announcementService.updateAnnouncement(announcement);
        return  R.success("修改成功");
    }

    @GetMapping("queryById/{AnnouncementId}")
    public R<Announcement> queryById(@PathVariable Integer AnnouncementId){
        Announcement announcement= announcementService.queryById(AnnouncementId);
        return  R.success(announcement);
    }

}
